package com.toocms.jinjuhang.ui.classify

import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.data.goods.ZoneListData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.ItemBinding

class ClassifyModel(application: Application) : BaseViewModel<BaseModel>(application) {
    val items = ObservableArrayList<MultiItemViewModel<BaseViewModel<*>>>()

    //    val itemBinding=ItemBinding.of<ClassifyItemModel>(BR.classifyItemModel, R.layout.item_classify)
    val itemBinding = ItemBinding.of<MultiItemViewModel<BaseViewModel<*>>> { itemBinding, _, item ->
        when (item.itemType) {
            Constants.RecyclerViewItemType.ITEM_TYPE_ZERO -> itemBinding.set(
                BR.classifyTitleLeftItemModel,
                R.layout.item_newest_classify_title_left
            )
            Constants.RecyclerViewItemType.ITEM_TYPE_ONE -> itemBinding.set(
                BR.classifyTitleRightItemModel,
                R.layout.item_newest_classify_title_right
            )
        }
    }

    init {
        requestData(Urls.Goods.ZONE_LIST, resultClass = ZoneListData::class.java, result = {
            it.list?.forEachIndexed { index, item ->
                if (0 == index % 2) {
                    items += ClassifyTitleLeftItemModel(this, ObservableField(item))
                } else {
                    items += ClassifyTitleRightItemModel(this, ObservableField(item))
                }
            }
        })
    }
}